package com.glela.controller;

import com.alibaba.fastjson.JSONObject;
import com.glela.common.constant.CommonConstants;
import com.glela.common.util.LinkUtil;
import com.glela.goods.mapper.CategoryMapper;
import com.glela.goods.model.Category;
import com.glela.platform.constant.ErrorEnum;
import com.glela.platform.model.response.ResponseBaseModel;
import com.glela.platform.model.response.ResponseResultModel;
import com.glela.platform.service.BaseRestrictionsService;
import com.glela.platform.util.ParameterUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/share")
public class ShareResultListController extends BaseController{

    @Autowired
    private BaseRestrictionsService baseRestrictionsService;

    @Autowired
    private CategoryMapper categoryMapper;

    @RequestMapping(value = "/resultList",method = RequestMethod.POST,consumes = "application/json")
    public ResponseEntity<?> resultList(@RequestBody JSONObject json){
        if (ParameterUtils.validParams(json.getString("userId"),json.getString("showType"))) {
            return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
        }

        String uri = "";
        String titleKeyWord = "";
        if (CommonConstants.SHARE_CATEGORY_TYPE == json.getIntValue("showType")){
            if (ParameterUtils.validParams(json.getString("keyWord"))){
                return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
            }
            uri = baseRestrictionsService.getBaseRestrictionsForCache("h5_select_goods_by_category").getName();//+"&categoryId="+json.getString("keyWord");
            String parentId = "";
            if (json.getString("keyWord").contains(",")){
                Category category = categoryMapper.selectCategoryBySonId(Integer.valueOf(json.getString("keyWord").split(",")[0]));
                uri +="parentId="+category.getId();
                titleKeyWord = category.getName();
            }else{
                Category category = categoryMapper.selectCategoryById(json.getIntValue("keyWord"));
                uri +="parentId="+category.getParentId()+"&id="+json.getString("keyWord");
                titleKeyWord = category.getName();
            }
        }else{
            titleKeyWord = json.getString("keyWord");
            uri = baseRestrictionsService.getBaseRestrictionsForCache("h5_search_goods").getName()+"searchKey="+json.getString("keyWord");
        }
        String h5_link = baseRestrictionsService.getBaseRestrictionsForCache("h5_link").getName();
        Map<String ,Object> map = new HashMap<>();
        StringBuffer shareBuff = new StringBuffer();
        shareBuff.append(h5_link).append(uri).append("&talentId="+json.getString("userId"));
        map.put("shareUrl", LinkUtil.generateSinaShortUrl(shareBuff.toString()));
        map.put("title","我为你精选了"+titleKeyWord+"，快来看看吧！");
        map.put("imageUrl","http://img.xzhuang.com/file/share/shareList/20180205112542383.jpg?imageView2/0/w/100/h/100");
        map.put("name","所有商品都是由我精挑细选出来的，希望有你喜欢哒~~");
        return new ResponseEntity<>(new ResponseResultModel<Map<String, Object>>(map), HttpStatus.OK);
    }
}
